package com.usian.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.common.fastdfs.FastDFSClient;
import com.usian.jwt.utils.threadlocal.ThreadLocalUtils;
import com.usian.model.base.BaseServiceImpl;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmMaterialDto;
import com.usian.model.media.pojos.WmMaterial;

import com.usian.model.media.pojos.WmNewsMaterial;
import com.usian.wemedia.mapper.WmMaterialMapper;
import com.usian.wemedia.mapper.WmNewsMaterialMapper;
import com.usian.wemedia.service.WmMaterialService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

@Service
@Log4j2
public class WmMaterialServiceImpl extends BaseServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FastDFSClient fastDFSClient;

    @Value("${fdfs.url}")
    private String beseUrl;

    @Autowired
    private WmNewsMaterialMapper wmNewsMaterialMapper;


    @Override
    public ResponseResult uploadPicture(MultipartFile file) {
        //1.检查参数
        if(file == null)return errorParam;

        //2.上传图片到fasfdfs
        String fileId = null;
        try {
            fileId = fastDFSClient.uploadFile(file);

        } catch (IOException e) {
            e.printStackTrace();
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }

        //3.保存素材数据到表中 wm_material

      //  Integer userId = ThreadLocalUtils.getUserId();

        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(ThreadLocalUtils.getUserId());
        wmMaterial.setUrl(fileId);
        wmMaterial.setIsCollection((short)0);
        wmMaterial.setType((short)0);
        wmMaterial.setCreatedTime(new Date());
        save(wmMaterial);
        //拼接图片路径
        return ResponseResult.okResult(beseUrl+fileId);
    }

    @Override
    public ResponseResult findList(WmMaterialDto dto) {
        //        if (dto==null)return errorParam;
        LambdaQueryWrapper<WmMaterial> qw = new LambdaQueryWrapper<>();
        qw.eq(WmMaterial::getUserId,ThreadLocalUtils.getUserId());
        qw.eq(dto.getIsCollection()!=null,WmMaterial::getIsCollection,dto.getIsCollection());
        qw.orderByDesc(WmMaterial::getCreatedTime);
        ResponseResult PageDate = getPageData(dto, qw);
        List<WmMaterial> list = (List<WmMaterial>) PageDate.getData();
        list.forEach(v->{v.setUrl(beseUrl+v.getUrl());});
        return ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult delPicture(Integer id) {
        if (id==null) return errorParam;
        WmMaterial material=getById(id);
        if (material ==null) return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"你删除的素材不存在");
        Integer count = wmNewsMaterialMapper.selectCount(Wrappers.<WmNewsMaterial>lambdaQuery().eq(WmNewsMaterial::getMaterialId, id));
        if (count > 0) return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"该素材已被引用");
        String url = material.getUrl();
        fastDFSClient.delFile(url);
        removeById(id);
        System.out.println(id);
        return succes;
    }

/*
    @Override
    public ResponseResult updateStatus(Integer id, Short type) {
        if (id ==null) return paramError();
        if (type ==null ||(type !=0 && type!=1)) return errorParam;
        update(Wrappers.<WmMaterial>lambdaUpdate().set(WmMaterial::getIsCollection,type).eq(WmMaterial::getId,id));
        return succes;
    }
}*/
@Override
public ResponseResult updateStatus(Integer id, Short type) {
    if (id == null)return errorParam;
    if (getById(id)==null)return errorParam;
    //修改状态
    update(Wrappers.<WmMaterial>lambdaUpdate().set(type!=null,WmMaterial::getIsCollection,type).eq(WmMaterial::getId,id));
    return succes;
}
}
